package com.zjdiepu.www.manage.api.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.system.OtherHolidaysVoMapper;
import com.zjdiepu.www.ibiz.system.IOtherHolidaysService;
import com.zjdiepu.www.model.system.dto.OtherHoliday;
import com.zjdiepu.www.model.system.ext.OtherHolidayExt;

@RestController
@RequestMapping(value= "OtherHolidaysController", produces="application/json;charset=UTF-8")
@Api(tags = {"节假日维护"}, produces="application/json;charset=UTF-8")
public class OtherHolidaysController extends BaseController{

	@Resource
	private IOtherHolidaysService otherHolidaysService;
	@Resource
	private OtherHolidaysVoMapper otherHolidaysVoMapper;
	/** 
	* @author zhangzj
	* @Title: getOtherHolidaysList 
	* @Description: 获取日期列表
	* @param @return    设定文件 
	* @return ApiResponse    返回类型 
	* @version V1.0
	* @throws 
	*/
	@ApiOperation(value = "获取日期列表", response = PageVo.class, httpMethod="POST", tags={"查询"})
    @RequestMapping(value = "selectByExt", method = RequestMethod.POST)
	public ApiResponse selectByExt(@ModelAttribute OtherHolidayExt otherHolidayExt){
		otherHolidayExt.notDeleted();
		PageVo page = getPage(otherHolidayExt);
		int records =otherHolidaysService.countByExt(otherHolidayExt);
		page.setRecords(records);
		if (records > 0) {
            List<OtherHoliday> overdueList = otherHolidaysService.selectByExt(otherHolidayExt);
            for (OtherHoliday otherHoliday : overdueList) {
				otherHolidaysVoMapper.mapper(otherHoliday);
			}
            page.setRows(overdueList);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
		return ApiResponse.success(page);
	}
	/** 
	* @author zhangzj
	* @Title: dateSplit 
	* @Description: 生成日期列表
	* @param @param begin
	* @param @param end
	* @param @return
	* @param @throws Exception    设定文件 
	* @return List<Date>    返回类型 
	* @version V1.0
	* @throws 
	*/
	private static List<OtherHoliday> dateSplit(Date begin,Date end) throws Exception{
		if (!begin.before(end))  
            throw new Exception("开始时间应该在结束时间之后");  
        Long spi = end.getTime() - begin.getTime(); 
        Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数  
        List<Date> dateList = new ArrayList<Date>();  
        dateList.add(end);  
        List<OtherHoliday> list=new ArrayList<OtherHoliday>();
        Date date=null;
        OtherHoliday ov= null;
        Calendar cal = Calendar.getInstance();
        for (int i = 1; i <= step; i++) {  
        	ov= new OtherHoliday();
        	ov.setId(String.valueOf(i));
        	date=new Date(dateList.get(i - 1).getTime() - (24 * 60 * 60 * 1000));
        	cal.setTime(date);
        	if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
        		ov.setDateType("2");
        	}else{
        		ov.setDateType("1");
        	}
        	ov.setDateH(date);
        	dateList.add(date);
        	list.add(ov);
        }  
		return list;
	}
	
	/** 
	* @author zhangzj
	* @Title: insert 
	* @Description: 保存近两年日期 
	* @param @return    设定文件 
	* @return ApiResponse    返回类型 
	* @version V1.0
	* @throws 
	*/
	@ApiOperation(value = "保存近两年日期", response = PageVo.class, httpMethod="POST", tags={"保存"})
    @RequestMapping(value = "insert", method = RequestMethod.POST)
	public ApiResponse insert(){
		Calendar calendar = Calendar.getInstance();  
		int thisyear = calendar.get(Calendar.YEAR);  
		int nextyear=thisyear+2;
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");  
		List<OtherHoliday> lists=null;
		try {
			Date begin=sf.parse(thisyear+"0101");
			Date end=sf.parse(nextyear+"0101");
			lists = dateSplit(begin, end);  
			otherHolidaysService.insert(lists);
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ApiResponse.success(true);
	}
	/** 
	* @author zhangzj
	* @Title: update 
	* @Description: 修改日期类型
	* @param @return    设定文件 
	* @return ApiResponse    返回类型 
	* @version V1.0
	* @throws 
	*/
	@ApiOperation(value = "修改日期类型", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "update", method = RequestMethod.POST)
	public ApiResponse update(@ModelAttribute OtherHoliday otherHoliday){
		otherHolidaysService.update(otherHoliday);
		return ApiResponse.success(true);
	}
	
	/** 
	* @author zhangzj
	* @Title: findDateTypeList 
	* @Description: 通过日期查询对应类型
	* @param @return    设定文件 
	* @return ApiResponse    返回类型 
	* @version V1.0
	* @throws 
	*/
	@ApiOperation(value = "通过日期查询对应类型", response = OtherHoliday.class, httpMethod="POST", tags={"查询"})
    @RequestMapping(value = "findDateTypeList", method = RequestMethod.POST)
	public ApiResponse findDateTypeList(@ModelAttribute List<OtherHoliday> listDate){
		List<OtherHoliday> list=otherHolidaysService.findDateTypeList(listDate);
		return ApiResponse.success(list);
	}
	/** 
	* @author zhangzj
	* @Title: getNextWorkDay 
	* @Description: N天后工作日日期
	* @param @param dateH
	* @param @param day
	* @param @return    设定文件 
	* @return ApiResponse    返回类型 
	* @version V1.0
	* @throws 
	*/
	@ApiOperation(value = "N天后工作日日期", response = OtherHoliday.class, httpMethod="POST", tags={"查询"})
    @RequestMapping(value = "getNextWorkDay", method = RequestMethod.POST)
	public ApiResponse getNextWorkDay(@ModelAttribute OtherHolidayExt oExt){//@ModelAttribute Date dateH,@ModelAttribute int num
		oExt.setBaseWhere();
		oExt.withOutPage();
		List<OtherHoliday> list=otherHolidaysService.selectByExt(oExt);
		Date date=null;
		if(list !=null && list.size()>0){
			for(int i=0;i<list.size();i++){
				if(list.get(i).getDateH().equals(oExt.getDateNH())){
					date=list.get(i+oExt.getNum()).getDateH();
					break;
				}
			}	
		}
		return ApiResponse.success(date);
	}
}
